package com.jianyun.jyzs.model;

import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.jianyun.jyzs.bean.OaAppBean;
import com.jianyun.jyzs.bean.OaCustomBean;
import com.jianyun.jyzs.bean.TopBrodCastBean;
import com.jianyun.jyzs.http.Api;
import com.jianyun.jyzs.http.CustomCallback;
import com.jianyun.jyzs.http.RetrofitHelper;
import com.jianyun.jyzs.model.imdoel.IOaCustomModel;
import com.jianyun.jyzs.utils.LoginCache;
import com.jianyun.jyzs.utils.SPUtils;

import java.util.ArrayList;
import java.util.List;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yangshijun on 2018/9/4.
 * 自定义应用界面的图标和权限
 */

public class OaCustomModel implements IOaCustomModel {

    private static OaCustomModel model;

    private OaCustomModel() {
    }

    public static OaCustomModel getInstance() {
        if (model == null) {
            model = new OaCustomModel();
        }
        return model;
    }


    @Override
    public void getOaCustomImg(String userId, String enterpriseCode, String appVersion, final OnOaImgListener onOaImgListener) {
        getFlowSwtich(userId);
        Api api = RetrofitHelper.builderErpRoot().create(Api.class);
        api.getOaIcon("MyAPPList", userId, enterpriseCode, "jyzs", "Android", appVersion)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Subscriber<Object>() {
                    @Override
                    public void onNext(Object o) {
                        Gson gson = new Gson();
                        String s = gson.toJson(o);
                        try {
                            //Log.i("test", "介些应用2：" + o.toString());
                            JsonParser parser = new JsonParser();
                            JsonArray jsonArray = parser.parse(s).getAsJsonArray();
                            List<OaAppBean> oaBeanList = new ArrayList<>();
                            for (JsonElement oabean :
                                    jsonArray) {
                                OaAppBean oaAppBean = gson.fromJson(oabean, OaAppBean.class);
                                oaBeanList.add(oaAppBean);
                            }
                            onOaImgListener.onSuccess2(oaBeanList);
                            //Log.i("test", "介些应用：" + oaBeanList.toString());
                        } catch (Exception e) {
                            try {
                                OaCustomBean oaCustomBean = gson.fromJson(s, OaCustomBean.class);
                                onOaImgListener.onSuccess(oaCustomBean);
                            } catch (Exception es) {
                                Log.e("err", "应用解析错误");
                                es.printStackTrace();
                                onOaImgListener.onFailed();
                            }

                        }


                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("err", "应用错误");
                        e.printStackTrace();
                        onOaImgListener.onFailed();
                    }

                    @Override
                    public void onCompleted() {

                    }
                });
    }

    @Override
    public void getTopcaset(String Url, final OnGetTopCastListener onGetTopCastListener) {

        Api api = RetrofitHelper.builderTopImghttp().create(Api.class);
        api.getTopBrodCasetImg(Url)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new CustomCallback<TopBrodCastBean>() {
                    @Override
                    protected void onResponse(TopBrodCastBean response) {
                        Log.i("test", "顶部论哦图：" + response.toString());
                        onGetTopCastListener.onGetTopImgSuccess(response);
                    }

                    @Override
                    protected void onException(String msg) {
                        Log.e("err", "轮播图错误：" + msg);
                    }
                });
    }


    /**
     * 获取用户审批分类开关初始化
     *
     * @param userID
     */
    public void getFlowSwtich(String userID) {
        Api api = RetrofitHelper.builderErpRoot().create(Api.class);
        api.getFlowSwtich("isStartFlowSwitch", userID, "")
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Subscriber<Object>() {
                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        LoginCache.getInstance().setFlowSwtich(-1);
                    }

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onNext(Object o) {
                        try {
                            Gson gson = new Gson();
                            String toJson = gson.toJson(o);
                            org.json.JSONObject jsonObject = new org.json.JSONObject(toJson);
                            //boolean result = jsonObject.getBoolean("result");

                                int isEnable = jsonObject.getInt("isEnable");
                                LoginCache.getInstance().setFlowSwtich(isEnable);


                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                });
    }

}
